package com.jinkaimen.kqb.ui.activity.h5toapp;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshBase;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshListView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.NetUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.BankBean;
import com.jinkaimen.kqb.bean.SearchBankBean;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.adapter.SearchBankAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class SearchBankActivity extends BaseActivity {
    public static final int REQUEST_CODE_BANK_NAME = 200;
    public static final int REQUEST_CODE_BANK_NAME_WRITE = 300;
    private String bankNo;
    private String branchCode;
    private int currentPage = 1;
    private String branchName;
    private EditText et_search;
    private TextView tv_search;
    private TextView tv_hint;
    private PullToRefreshListView mSearchBankList;
    private ListView mListView;
    private SearchBankAdapter searchBankAdapter;
    private List<BankBean> mDataList;
    //    private LinearLayout ll_activity_write_bank;
    private TextView btn_activity_write_bank;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setLeftBtnIcon(R.mipmap.back)
                .setTitleText("搜索支行");
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        bankNo = callIntent.getStringExtra("bankNo");
        branchCode = callIntent.getStringExtra("branchCode");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_search_bank;
    }

    @Override
    protected void initChildView() {
        et_search = (EditText) findViewById(R.id.et_search);
        tv_search = (TextView) findViewById(R.id.tv_search);
        mSearchBankList = (PullToRefreshListView) findViewById(R.id.plv_activity_search_bank_list);
        mSearchBankList.setPullLoadEnabled(false);
        mSearchBankList.setScrollLoadEnabled(true);
        mListView = mSearchBankList.getRefreshableView();
        tv_hint = (TextView) findViewById(R.id.tv_hint);
//        ll_activity_write_bank = (LinearLayout) findViewById(R.id.ll_activity_write_bank);
        btn_activity_write_bank = (TextView) findViewById(R.id.btn_activity_write_bank);
        mSearchBankList.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initChildData() {
        et_search.setHint("请输入支行关键字");
        et_search.setSelection(0);
        tv_hint.setVisibility(View.VISIBLE);
        mDataList = new ArrayList<>();
        searchBankAdapter = new SearchBankAdapter(this, mDataList);
        mListView.setAdapter(searchBankAdapter);
        currentPage = 1;
    }


    private void netWork(String branchName) {
        branchName = TextUtils.isEmpty(branchName) ? "" : branchName;
        branchCode = SharedPreferencesUtils.getString("bankUseCode", "");
        if (TextUtils.isEmpty(branchCode)) {
            UIUtils.showToast("城市编号不能为空");
            return;
        }
        ApiManager.getApi().searchBank(
                "HSS001034",
                branchName,
                bankNo,
                branchCode,
                currentPage,
                10

        ).enqueue(new NetCallBack<SearchBankBean>() {
            @Override
            protected void onSuccess(Call<SearchBankBean> call, Response<SearchBankBean> response) {
                if (currentPage == 1) {
                    mDataList.clear();
                }
                if (response != null) {


                    if (response != null && response.body() != null) {
                        mSearchBankList.setVisibility(View.VISIBLE);
//                    ll_activity_write_bank.setVisibility(View.GONE);
                        List<BankBean> bankBeanList = response.body().result.records;
                        if (bankBeanList != null)
                            mDataList.addAll(bankBeanList);
                        if (currentPage != 1 && bankBeanList.size() <= 0) {
                            UIUtils.showToast("没有更多内容");
                        }
                    } else {
                        mSearchBankList.setVisibility(View.GONE);
//                    ll_activity_write_bank.setVisibility(View.VISIBLE);
                    }
                    searchBankAdapter.notifyDataSetChanged();
                }
            }

            @Override
            protected void hiddenProgress() {
                if (currentPage == 1) {
                    mSearchBankList.onPullDownRefreshComplete();
                } else {
                    mSearchBankList.onPullUpRefreshComplete();
                }

            }
        });

    }

    @Override
    protected void setChildViewListener() {
        btn_activity_write_bank.setOnClickListener(this);
        tv_search.setOnClickListener(this);
        mSearchBankList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage = 1;
                if (!NetUtils.isNetWorkConnection()) {
                    UIUtils.showToast("请检查您的网络设置");
                    return;
                }
                mSearchBankList.resetHasMore();
                currentPage = 1;
                netWork(branchName);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage++;
                netWork(branchName);
            }
        });
        mSearchBankList.doPullRefreshing(true, 800);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                intent.putExtra("bankName", mDataList.get(position).branchName);
                intent.putExtra("branchCode", mDataList.get(position).branchCode);

                setResult(RESULT_OK, intent);
                ActivityUtils.finishActivity(SearchBankActivity.this);
            }
        });

    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.EVENT_CODE_BANKNAME:
                ActivityUtils.finishActivity(SearchBankActivity.this);
                break;

        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_search:
                branchName = et_search.getText().toString().trim();
                if (!TextUtils.isEmpty(branchName)) {
                    currentPage = 1;
                    netWork(branchName);
                } else {
                    UIUtils.showToast("请输入搜索关键字");
                }
                break;
            case R.id.btn_activity_write_bank:
// TODO: 2017/6/8 填写
//                ActivityUtils.startActivityForResult(SearchBankActivity.this, WriteBankActivity.class, REQUEST_CODE_BANK_NAME_WRITE);
                break;
        }
    }
}
